package com.vidyu.businesslayer.businesscomponents.trackingmerger.impl;

import com.vidyu.businesslayer.businesscomponents.Constants;
import com.vidyu.businesslayer.businesscomponents.trackingmerger.MergeOnceWrapper;
import org.apache.log4j.Logger;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.context.ApplicationContext;
import org.springframework.scheduling.quartz.QuartzJobBean;
import org.springframework.stereotype.Component;

/**
 * Created by IntelliJ IDEA.
 * User: eyalsa
 * Date: 05/03/12
 * Time: 09:10
 * Wrapper for fetch once using quartz
 */
@Component
public class MergeOnceWrapperImpl extends QuartzJobBean implements MergeOnceWrapper {

    private static final Logger logger = Logger.getLogger(MergeLoopWrapperImpl.class);

    @Override
    public void executeInternal(JobExecutionContext context) throws JobExecutionException {
        try {
            ApplicationContext applicationContext = (ApplicationContext) context.getScheduler().getContext().get("applicationContext");
            TrackingMergerImpl obj = applicationContext.getBean(TrackingMergerImpl.class);
            JobDataMap jobDataMap = context.getMergedJobDataMap();
            String testStr = (String) jobDataMap.get(Constants.TEST);
            obj.executeOnce(testStr);
        }
        catch (JobExecutionException je) {
            logger.fatal("Exception in MergeOnceWrapperImpl", je);
            throw je;
        } catch (Exception e) {
            logger.fatal("Exception in MergeOnceWrapperImpl", e);
        }
    }
}
